package com.tianya.forum.controller;


import com.tianya.forum.common.AppResult;
import com.tianya.forum.common.ResultCode;
import com.tianya.forum.exception.ApplicationException;
import com.tianya.forum.model.Board;
import com.tianya.forum.services.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;



@Slf4j
@Api(tags = "版块接口")
@RestController
@RequestMapping("/board")
public class BoardController {

    @Value("${tianya-forum.index.board-num}")
    private Integer indexNum;
    @Resource
    private IBoardService boardService;

    /**
     * 主页中显示的板块
     * @return
     */
    @ApiOperation(value = "主页显示正常状态版块")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        List<Board> boards = boardService.selectByNum(indexNum);
        return AppResult.success(boards);
    }

    /**
     *  显示对应版块下帖子数量
     * @param id 版块ID
     * @return
     */
    @ApiOperation(value = "主页显示对应版块下帖子数量")
    @GetMapping("/getById")
    public AppResult<Board> getById(@ApiParam(value = "版块ID") @RequestParam(value = "id") @NonNull Long id){
        //调用Service
        Board board = boardService.selectById(id);
        //对查询结果校验
        if(board == null || board.getDeleteState() ==1){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return AppResult.success(board);
    }
}
